<?php

namespace App\Http\Controllers\WebPage\Admin;


use App\Http\Models\Article;
use App\Http\Models\ArticleTag;
use App\Services\Utils\Transaction;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Libs\Bos\BosClientProxy;


class ArticleTypeController extends Controller
{
    public function index()
    {
        $tags = ArticleTag::where('super_tag_id', null)->get();

        return view('web-admin.news.type-tag', compact('articles', 'tags'));
    }

    /**
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Exception
     */
    public function store(Request $request)
    {
        $data = $request->all();

        /** @var $create ArticleTag */
        $create = Transaction::handle(function () use ($data) {

            $tags = ArticleTag::safeCreate($data);

            return $tags;
        });

        if ($create) {
            return redirect()->back()->with('success', '分类标签： "' . $create->name . '" 添加成功');
        } else {
            return redirect()->back()->withInput()->withErrors(' 添加失败,请重试');
        }

    }

    public function edit($id)
    {

        $tags = ArticleTag::find($id);


        return $tags;

    }

    /**
     * @param Request $request
     * @param $id
     *
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Exception
     */
    public function update(Request $request, $id)
    {
        $tags = ArticleTag::find($id);

        $data = $request->all();

        $update = Transaction::handle(function () use ($tags, $data) {

            $update_tag = $tags->safeUpdate($data);

            return $update_tag;
        });


        if ($update) {
            //todo 路由中间件不要重复使用web;
            return redirect()->back()->with('success', '更新成功');

        } else {
            return redirect()->back()->withInput()->withErrors('更新失败,请重试');
        }

    }

    public function destroy($id)
    {

        $tag = ArticleTag::find($id);

        if (count($tag->articles) > 0) return redirect()->back()->withInput()->withErrors(' 该类别下存在新闻，请先删除对应的文章');

        $del = $tag->delete();

        if ($del) {
            return redirect()->back()->with('success', '删除成功');
        } else {
            return redirect()->back()->withInput()->withErrors(' 删除失败,请重试');
        }

    }

    public function articleList()
    {
        $articles = (new Article)
            ->orderBy('id', 'DESC')
            ->paginate(5);

        return view('bym_news', compact('articles'));
    }

    public function articleDetail($id)
    {
        $article = Article::find($id);
        $previous = Article::where('id', '<', $id)->orderBy('id', 'DESC')->first();
        $next = Article::where('id', '>', $id)->orderBy('id', 'ASC')->first();

        return view('bym_article', compact(
            'article',
            'previous',
            'next'
        ));
    }

    /**
     * @param Request $request
     *
     * @return false|string
     * @throws \Exception
     */
    public function uploadArticleImg(Request $request)
    {
        $uploadFile = $request->file('upfile');

        $img_origin_name = $uploadFile->getClientOriginalName();
        list($img_pre, $img_ext) = explode(".", $img_origin_name);
        $image_key = $img_pre . time() . '.' . $img_ext;

        $bosClient = new BosClientProxy();
        $image_path = $uploadFile->getRealPath();
        $bosClient->client->putObjectFromFile('panx-website', 'article/content/' . $image_key, $image_path);

        $resArr = [
            "originalName" => '',
            "name"         => '',
            "url"          => 'http://panx-website.gz.bcebos.com/article/content/' . $image_key,
            "size"         => 1,
            "type"         => '.' . $img_ext,
            "state"        => "SUCCESS",
        ];

        return json_encode($resArr);
    }
}

